home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / transformations / transforms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.1 KB  |  137 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* transforms.c - draw a triangle under the effect of a rotation, 
  19.  *        a translation and a scale
  20.  *
  21.  *    Escape key    - exit program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glut.h>
  25.  
  26. #include <stdio.h>
  27.  
  28. /*  Function Prototypes  */
  29.  
  30. GLvoid  initgfx( GLvoid );
  31. GLvoid  keyboard( GLubyte, GLint, GLint );
  32. GLvoid  drawScene( GLvoid );
  33.  
  34. void drawTriangle( GLfloat red, GLfloat green, GLfloat blue );
  35.  
  36. void printHelp( char * );
  37.  
  38. /* Global Definitions */
  39.  
  40. #define KEY_ESC    27    /* ascii value for the escape key */
  41.  
  42. void
  43. main( int argc, char *argv[] )
  44. {
  45.     GLsizei width, height;
  46.  
  47.     glutInit( &argc, argv );
  48.  
  49.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  50.     height = glutGet( GLUT_SCREEN_HEIGHT );
  51.     glutInitWindowPosition( width / 4, height / 4 );
  52.     glutInitWindowSize( width / 2, height / 2 );
  53.     glutInitDisplayMode( GLUT_RGBA );
  54.     glutCreateWindow( argv[0] );
  55.  
  56.     initgfx();
  57.  
  58.     glutKeyboardFunc( keyboard );
  59.     glutDisplayFunc( drawScene ); 
  60.  
  61.     printHelp( argv[0] );
  62.  
  63.     glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  64.  
  65.     glutMainLoop();
  66. }
  67.  
  68. void
  69. printHelp( char *progname )
  70. {
  71.     fprintf(stdout, "\n%s - demonstrates modeling transformations\n\n"
  72.         "Escape Key        - exit the program\n\n",
  73.         progname);
  74. }
  75.  
  76. GLvoid
  77. initgfx( GLvoid )
  78. {
  79.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  80.     glShadeModel( GL_FLAT );
  81. }
  82.  
  83. GLvoid 
  84. keyboard( GLubyte key, GLint x, GLint y )
  85. {
  86.     switch (key) {
  87.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  88.         exit(0);
  89.     }
  90. }
  91.  
  92. GLvoid
  93. drawTriangle( GLfloat red, GLfloat green, GLfloat blue )
  94. {
  95.     glColor3f( red, green, blue );
  96.     glBegin( GL_TRIANGLES );
  97.         glVertex2f( -0.5, -0.5 );
  98.         glVertex2f( 0.5, -0.5 );
  99.         glVertex2f( 0.0, 0.5 );
  100.     glEnd();
  101. }
  102.  
  103. GLvoid
  104. drawScene( GLvoid )
  105. {
  106.     glClear( GL_COLOR_BUFFER_BIT );
  107.  
  108.     /* draw a black triangle centered at the origin */
  109.     drawTriangle( 0.0, 0.0, 0.0 );
  110.  
  111.     /* Draw x and y axes to show the origin */
  112.     XYaxes();
  113.  
  114.     /* move over a bit and draw another triangle */
  115.     glTranslatef( 0.5, 0.0, 0.0 );
  116.     drawTriangle( 0.3, 0.3, 0.3 ); 
  117.  
  118.     /* move over a bit and draw a rotated triangle */
  119.     glTranslatef( 0.5, 0.0, 0.0 );
  120.  
  121.     /* Rotate 15 degrees counter-clockwise around the positive Z axis */
  122.     glRotatef( -15.0, 0.0, 0.0, 1.0 );
  123.     drawTriangle( 0.7, 0.7, 0.7 );
  124.  
  125.     /* move over a bit and draw a scaled triangle */
  126.     glTranslatef( 0.5, 0.0, 0.0 );
  127.  
  128.     /* Scale the triangle by one-half in all dimensions, and
  129.      * reflect about the x axis
  130.      */
  131.     glScalef( -0.5, 0.5, 0.5 );
  132.     drawTriangle( 1.0, 1.0, 1.0 );     /* draw white triangle */
  133.     XYaxes();
  134.  
  135.     glFlush();
  136. }
  137.